<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\facade\View;
use think\facade\Db;
use think\captcha\facade\Captcha;

use app\common\model\Order as OrderModel;
use app\common\model\Lists as ListsModel;
use app\common\model\Users as UsersModel;

use app\admin\validate\Order as validateOrder;
/**
 * @title 订单
 * @description 订单管理
 */
class Order extends Base
{
    protected $middleware = [
        'CountTime' => ['only' 		=> ['order_confirm'] ],
    ];

    private $user=[];

    function __construct(){

        parent::__construct();
        $param = input('param.');
        $token = isset($param['token'])?$param['token']:'';
        if($token) {
            $res = checkToken($param['token']);
            if ($res['code']==2) {
                return dyajaxReturn(-1, $res['msg']);
            }
            $this->user = json_decode($res['data'], true);
        }
    }

    /**
     * @title 添加订单
     * @description 添加订单
     * @author 开发者 唐寅
     * @url /api/Order/addOrder
     * @method GET POST
     *
     * @param name:token type:varchar require:1 default:0 other: desc:token
     * @param name:lists_id type:varchar require:1 default:0 other: desc:培训报名/职称学历的id
     *
     */
    public function addOrder(){
        $param = input('param.');
        if(!isset($param['lists_id']) || $param['lists_id'] == ''){
            return dyajaxReturn(0,'lists_id不能为空');
        }

        $validate = new validateOrder();
        $result = $validate->scene('create')->batch(false)->check($param);
        if(!$result){
            return dyajaxReturn(0,$validate->getError());
        }
        $OrderModel = new OrderModel();
        $param['act'] = 'add';
        $param['user_id'] = $this->user['user_id'];
        $res = $OrderModel->handle($param);

        return dyajaxReturn($res['code'],$res['msg'],$res['data']);
    }


    /**
     * @title 确定订单内容
     * @description duration 时长 effective_date	截止日期 sign_up_num	报名人数

     * @author 开发者 唐寅
     * @url /api/Order/order_confirm
     * @method GET POST
     *
     * @param name:token type:varchar require:1 default:0 other: desc:token
     * @param name:order_id type:varchar require:1 default:0 other: desc:订单id
     *
     */
    public function order_confirm(){
        $param = input('param.');
        if(!isset($param['order_id']) || $param['order_id'] == ''){
            return dyajaxReturn(0,'order_id不能为空');
        }

        $order_id = $param['order_id'];
        $OrderModel = new OrderModel();
        $info = $OrderModel->where('id','=',$order_id)->find();
        if($info['pay_status'] != 1){
            return dyajaxReturn(0,'此订单不是待支付订单');
        }

        $ListsModel = new ListsModel();
        $lists_one = $ListsModel->where('id', '=', $info['lists_id'])->find();
        $info['duration'] = $lists_one['duration'];
        $info['effective_date'] = $lists_one['effective_date'];
        $info['sign_up_num'] = $lists_one['sign_up_num'];
        $info['add_time'] = $info['add_time'] + 1800;
        $info['order_id'] = $order_id;

        return dyajaxReturn(1,'返回信息',$info);

    }

    /**
     * @title 确定订单提交
     * @description pay_code 的值写 weixin

     * @author 开发者 唐寅
     * @url /api/Order/order_confirm_form
     * @method GET POST
     *
     * @param name:token type:varchar require:1 default:0 other: desc:token
     * @param name:id type:varchar require:1 default:0 other: desc:订单id
     * @param name:take_name type:varchar require:1 default:0 other: desc:收货人姓名
     * @param name:take_mobile type:varchar require:1 default:0 other: desc:收货人联系方式
     * @param name:take_address type:varchar require:1 default:0 other: desc:请输入收货人地址
     * @param name:pay_code type:varchar require:1 default:0 other: desc:固定写死weixin
//     * @param name:pay_status type:varchar require:1 default:0 other: desc:当价格为0时候传，3=支付成功
     *
     */
    public function order_confirm_form(){
        $param = input('param.');

        $validate = new validateOrder();
        $result = $validate->scene('edit')->batch(false)->check($param);
        if(!$result){
            return dyajaxReturn(0,$validate->getError());
        }
        $OrderModel = new OrderModel();
        $ord_one = $OrderModel -> where('id','=',$param['id'])->find();
        if(!$ord_one['id']){
            return dyajaxReturn(0,'没有此订单');
        }
        if( $ord_one['add_time'] + 1800 <= time() ){
            return dyajaxReturn(0,'订单支付时间已超时');
        }

        if($ord_one['price'] == 0){
            $param['pay_status'] = 3;
            $param['pay_time'] = time();
            $param['act'] ='edit';
            $param['user_id'] = $this->user['user_id'];
            $res = $OrderModel->handle($param);
            return dyajaxReturn(2,$res['msg'],$res['data']);
        }
        $param['act'] = 'edit';
        $param['pay_name'] = '微信小程序';
        $param['user_id'] = $this->user['user_id'];
        $res = $OrderModel->handle($param);
        return dyajaxReturn($res['code'],$res['msg'],$res['data']);

    }


    //支付成功页面
    public function successpage(){


        return View::fetch('index/successpage');
    }

}
